package com.p2p.backend.controller;

import java.text.ParseException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.p2p.backend.service.OplogService;
import com.p2p.backend.util.PageUtils;
import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.exception.BizException;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Money;
import com.p2p.base.model.Oplog;
import com.p2p.base.util.DateUtil;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.LoanRepay;
import com.p2p.front.result.LoanRepayResult;
import com.p2p.front.service.LoanInfoService;
import com.p2p.front.service.LoanRepayBizService;
import com.p2p.front.service.LoanRepayService;
import com.p2p.front.service.model.BizReturnAdvanceModel;
import com.p2p.front.task.RepayTask;
import com.p2p.front.web.form.TenderRepayForm;

/**
 * @author ruiqi
 *
 */
@LoginNeedless
@Controller
@RequestMapping("/tenderrepay")
public class TenderRepayController extends BaseController {

	@Resource
	RepayTask repayTask;

	@Resource
	protected LoanRepayService loanRepayService;
	
	@Resource
	private LoanInfoService loanInfoService;
	
	@Resource
	private LoanRepayBizService loanRepayBizService;

	@RequestMapping("/doTestReturn")
	public String doReturnTest(String tenderDate) {
		Date date = null;
		try {
			date = DateUtil.parseYYYYMMDDStr(tenderDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (tenderDate != null) {
			repayTask.flowLoanTenderReturn(date);
		}

		return "/tender/testReturnIndex";
	}

	/*@RequestMapping("/doTestRepay")
	public String doRepayTest(Long rid) {
		if (rid != null) {
			repayTask.nomalLoanTenderReturn(rid);
		}
		return "/tender/testReturnIndex";
	}*/

	@PermissionNeed("repay_list")
	@RequestMapping("/list")
	public void list(TenderRepayForm form, Model m) {
		PageInfo page = loanRepayService.listPage(form);
		m.addAttribute("res", 1);
		m.addAttribute("data", PageUtils.data(page));
	}

	@PermissionNeed("repay_management")
	@RequestMapping("/pay")
	public void pay(Long rid,boolean needRepay,Model model, HttpServletRequest request) {
		/*LoanRepayResult repayResult = null;
		LoanRepay loanRepay = loanRepayService.getByRid(rid);
		if(loanRepay!=null){
			LoanInfo loanInfo = loanInfoService.getLoanInfoById(loanRepay.getLid());
			if(loanInfo.getIsFlow()==0){
				repayResult = loanRepayService.doRepay2(loanRepay.getRid(), false, loanInfo);
				if(repayResult.isSuccess()){
					model.addAttribute("res", 1);
					return;
				}
				
			}
		}
		model.addAttribute("res", 0);
		if(repayResult!=null){
			model.addAttribute("errorCode", repayResult.getResultCode());
		}*/
		LoanRepayResult result = loanRepayBizService.doRepayForNomal(rid, new Money(),needRepay);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.FUNDS_REPAY);
		oplog.setAction(Action.PAY);
		oplog.setParams("rid="+rid);
		
		if(result.isSuccess()){
			model.addAttribute("res", 1);
			oplog.setRemark("ok");
		}else{
			model.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			model.addAttribute("msg",msg);
			oplog.setRemark("failed");
		}
		OplogService.log(oplog);
	}
	@PermissionNeed("repay_management")
	@RequestMapping("/payPrompt")
	public String payPrompt(Long rid,boolean needRepay,Model model) {
		LoanRepay loanRepay = loanRepayService.getByRid(rid);
		LoanRepayResult result = loanRepayBizService.doRepayForNomalCheck(loanRepay);
		if(result.isSuccess()){
			model.addAttribute("res", 1);
			LoanInfo loanInfo = loanInfoService.getLoanInfoById(loanRepay.getLid());
			model.addAttribute("loanRepay",loanRepay);
			model.addAttribute("loanInfo",loanInfo);
		}else{
			model.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			model.addAttribute("msg",msg);
		}
		return "loanRepay/payPrompt";
	}
	
	
	@PermissionNeed("repay_management")
	@RequestMapping("/payAdvancePrompt")
	public String payAdvancePrompt(Long lid,boolean needRepay,Model model) {
		BizReturnAdvanceModel bizReturnAdvanceModel = loanRepayBizService.getBizReturnAdvanceModel(lid);
		LoanRepayResult result = loanRepayBizService.doRepayForAdvance2Check(bizReturnAdvanceModel);
		if(result.isSuccess()){
			model.addAttribute("res", 1);
			model.addAttribute("bizReturnAdvanceModel", bizReturnAdvanceModel);
		}else{
			model.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			if(StringUtils.isBlank(msg)){
				model.addAttribute("msg",code);
			}else{
				model.addAttribute("msg",msg);
			}
		}
		return "loanRepay/payAdvancePrompt";
	}
	
	@PermissionNeed("repay_management")
	@RequestMapping("/payAdvance")
	public void payAdvance(Long lid,boolean needRepay,double fine,Model model, HttpServletRequest request) {
		LoanRepayResult result = loanRepayBizService.doRepayForAdvance2(lid, new Money(fine),needRepay);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.FUNDS_REPAY);
		oplog.setAction(Action.PAY_ADVANCE);
		oplog.setParams("lid="+lid);
		if(result.isSuccess()){
			model.addAttribute("res", 1);
			oplog.setRemark("ok");
		}else{
			model.addAttribute("res", 0);
			String code = result.getResultCode();
			String msg = BizException.getGlobalMessage(code);
			if(StringUtils.isBlank(msg)){
				model.addAttribute("msg",code);
			}else{
				model.addAttribute("msg",msg);
			}
			oplog.setRemark("failed");
		}
		OplogService.log(oplog);
	}
}
